package com.anhry.app.safety.util;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;

import com.anhry.app.safety.util.bean.ExcelHeaderBean;



public abstract class ExcelServiceAbstract {
	
	/**
	 *  导出excel,单行表格头部,适用于统计分析
	 * @param htmllist
	 * @param sumlist
	 * @param response
	 * @param bean
	 * @param title
	 * @param cell0
	 * @param is
	 * @return
	 * @throws Exception
	 */
	public HSSFWorkbook ExcelExportByArray(List<Object[]> htmllist,List<Object[]> sumlist,HttpServletResponse response,ExcelHeaderBean[] bean,String title,int cell0,Integer...is) throws Exception {
		return this.ExcelExportByArray(htmllist,sumlist,response, new ExcelHeaderBean[][]{bean},title,cell0,is) ;
	}
	
	/**
	 *  导出excel,单行表格头部,合并相同列，适用于标准化
	 * @param htmllist
	 * @param sumlist
	 * @param response
	 * @param bean
	 * @param title
	 * @param cell0
	 * @param is
	 * @param rowIndex1	起始列
	 * @param rowIndex2	终止列
	 * @return
	 * @throws Exception
	 */
	public HSSFWorkbook ExcelExportIrregular(List<Object[]> htmlList,List<Object[]> sumlist,HttpServletResponse response,ExcelHeaderBean[][] bean,int rowIndex1,int rowIndex2,String title,int cell0,int rowNums,Integer...is) throws Exception {
		return this.ExcelExportIrregular(htmlList, sumlist, response, bean, rowIndex1, rowIndex2, title, cell0, rowNums, is);
	}
	
	/**
	 *  导出excel,多行表格头部,适用于统计分析
	 * @param htmlList  统计结果list<Object[]>
	 * @param sumlist   统计合计list<Object[]>
	 * @param response  
	 * @param bean      表格展示,头部信息bean ,行合并后占用的列用空格占据.
	 * @param title     导出excel表名
	 * @param cell0     首行是否合并   -1表示不合并,  若cell0==htmlList.size() 标示全部合并    htmlList.size>cell0   则标示以cell0为单位进行合并
	 * @param is        由于有的htmlList中的装的数组,有的列不需要,则此参数为数组的下标
	 * @return
	 * @throws Exception
	 */
	protected abstract HSSFWorkbook ExcelExportByArray(List<Object[]> htmlList,List<Object[]> sumlist,HttpServletResponse response,ExcelHeaderBean[][] bean,String title,int cell0,Integer...is) throws Exception ;
	
	
	public HSSFWorkbook ExcelExportByPojo(List htmllist,HttpServletResponse response,ExcelHeaderBean[] bean,String title,int cell0,Class clazz,String []fieldArray) throws Exception {
		return this.ExcelExportByPojo(htmllist,null,response, new ExcelHeaderBean[][]{bean},title,cell0,clazz,fieldArray)  ;
	}
	
	public HSSFWorkbook ExcelExportByPojo(List htmllist,List sumList,HttpServletResponse response,ExcelHeaderBean[] bean,String title,int cell0,Class clazz,String []fieldArray) throws Exception {
		return this.ExcelExportByPojo(htmllist,sumList,response, new ExcelHeaderBean[][]{bean},title,cell0,clazz,fieldArray)  ;
	}
	
	/**
	 * 导出excel,其中结果集里面装的是JavaBean
	 * @param htmllist 结果集
	 * @param sumList 合计结果
	 * @param response   
	 * @param bean   表头部bean
	 * @param title  excel名称
	 * @param cell0  首行合并
	 * @param clazz  htmllist里面装的bean的Class
	 * @param fieldArray 各列在bean中对应的属性.
	 * @return
	 * @throws Exception
	 */
	protected abstract HSSFWorkbook ExcelExportByPojo(List htmllist,List sumList,HttpServletResponse response,ExcelHeaderBean[][] bean,String title,int cell0,Class clazz,String[] fieldArray) throws Exception ;
	
	/**
	 * 初始化表头部信息
	 * @param sheet
	 * @param style
	 * @param bean
	 * @param title
	 * @param width
	 */
	protected void initHeader(HSSFSheet sheet, HSSFCellStyle style,ExcelHeaderBean[][] bean,String title,int width) {
		HSSFRow row0 = sheet.createRow(0);
		row0.setHeight((short)340) ;
		HSSFCell cell = row0.createCell((short)0) ;
		cell.setCellValue(title) ;
		cell.setCellStyle(style) ;
		sheet.addMergedRegion(new Region(0,(short)0,0,(short)(width-1)));  //Region(int rowFrom, short colFrom, int rowTo, short colTo) 
			for(int i=0;i<bean.length;i++){
			HSSFRow row1 = sheet.createRow(i+1);
			for(short j=0,s=0;j<bean[i].length;j++,s++){
				sheet.setColumnWidth(j,(short)(35*120) ); //设置每列的宽度
				createCell(row1, s, bean[i][j].getColName(), style);
				if(bean[i][j].getColSpan()>1){//设置合并
					sheet.addMergedRegion(new Region(i+1,s,i+bean[i][j].getRowSpan(),(short)(s+bean[i][j].getColSpan()-1)));// 
					s += bean[i][j].getColSpan()-1 ; //合并列不用再创建
				}
				if(bean[i][j].getRowSpan()>1){
					sheet.addMergedRegion(new Region(i+1,j,i+bean[i][j].getRowSpan(),(short)(j+bean[i][j].getColSpan()-1)));// 
				}
			}
		}
	}
	/**
	 * 初始化尾部
	 * @param sheet
	 * @param style
	 * @param sumlist
	 * @param rowNum
	 * @param width
	 */
	protected void initEnd(HSSFSheet sheet,HSSFCellStyle style,List<Object[]> sumlist,int rowNum,int width) {
		if(sumlist !=null){
			HSSFRow row = sheet.createRow(rowNum);
			Object obj[] = sumlist.get(0) ;
			sheet.addMergedRegion(new Region(rowNum,(short)0,rowNum,(short)(width-obj.length-1)));// 
			createCell(row, 0, "合计", style) ;
			for(int i=width-obj.length,j=0;i<width;i++,j++){
				createCell(row, i, obj[j], style) ;
			}
		}
	}

	protected void createCell(HSSFRow row, int column, Object value, HSSFCellStyle style) {
		HSSFCell cell = row.createCell((short)column);
		cell.setCellValue(String.valueOf(value==null?"":value));
		cell.setCellStyle(style);
	}

}
